import sun.reflect.generics.tree.Tree;

import java.util.*;

public class BinaryTree {


    static class TreeNode{
        public char val;
        public TreeNode left;
        private TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }



    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');


        A.left = B;
        A.right =C;
        B.left  =D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;

    }

    //二叉树的前序遍历
    public void preOrder(TreeNode root){
        if (root==null){
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);

    }


    public List<TreeNode> preOrder2(TreeNode root){
        List<TreeNode> ret = new ArrayList<>();
        if (root==null){
            return ret;
        }
        ret.add(root);
        List<TreeNode> leftTree = preOrder2(root.left);
        ret.addAll(leftTree);
        List<TreeNode> rightTree = preOrder2(root.right);
        ret.addAll(rightTree);
        return ret;

    }

    //中序遍历
    public void inOrder(TreeNode root){
        if (root==null){
            return;
        }
        preOrder(root.left);
        System.out.print(root.val+" ");
        preOrder(root.right);
    }

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if (root==null){
            return list;
        }
        List<Integer> left = inorderTraversal(root.left);
        list.addAll(left);
        list.add((int) root.val);
        List<Integer> right = inorderTraversal(root.right);
        list.addAll(right);
        return list;
    }





    public void postOrder(TreeNode root){
        if (root==null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }

    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if (root==null){
            return list;
        }
        List<Integer> left = postorderTraversal(root.left);
        list.addAll(left);
        List<Integer> right = postorderTraversal(root.right);
        list.addAll(right);
        list.add((int) root.val);
        return list;

    }


    public int size = 0;
    public int nodeSize(TreeNode root){
        if (root==null){
            return 0;
        }
        size++;
        nodeSize(root.left);
        nodeSize(root.right);
        return size;


    }

    public int nodeSize1(TreeNode root){
        if (root==null){
            return 0;
        }
        return 1 + nodeSize1(root.left)+nodeSize1(root.right);


    }

    //叶子节点
    public int leafSize = 0;
    public void getLeafSize(TreeNode root){
        if (root==null){
            return;
        }
        if (root.left==null && root.right==null){
            leafSize++;
        }
        getLeafSize(root.left);
        getLeafSize(root.right);

    }

    public int getLeafSize2(TreeNode root){
        if (root==null){
            return 0;
        }
        if (root.left==null && root.right==null){
            return 1;
        }
        return getLeafSize2(root.left) + getLeafSize2(root.right);

    }


    //获取第k层节点的个数
    public int getKLevelCount(TreeNode root,int k){
        if (root==null){
            return 0;
        }
        if (k==1){
            return 1;
        }
        return getKLevelCount(root.left,k-1)
                +getKLevelCount(root.right,k-1);

    }




    //查找是否存在value
    public boolean find(TreeNode root,char key){
        if (root == null){
            return false;
        }
        if (root.val == key){
            return true;
        }

        boolean leftVal = find(root.left,key);
        if (leftVal == true){
            return true;
        }

        boolean rightVal = find(root.right,key);
        if (rightVal == true){
            return true;
        }
        return false;




    }

    public boolean isSameTree(TreeNode p, TreeNode q) {
        //先判断结构
        if (p==null&&q!=null || p!=null&&q==null){
            return false;
        }
        if (p==null&&q==null){
            return true;
        }
        //一定是两个节点都不为空
        if (p.val!=q.val){
            return false;
        }

        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);

    }


    public boolean isSubtree(TreeNode root, TreeNode subRoot) {

        if (root==null){
            return false;
        }
        if (isSameTree(root,subRoot)){
            return true;
        }
        if (isSubtree(root.left,subRoot)){
            return true;
        }
        if (isSubtree(root.right,subRoot)){
            return true;
        }
        return false;

    }


    public TreeNode invertTree(TreeNode root) {
        if (root==null){
            return null;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left);
        invertTree(root.right);
        return root;

    }

    //树的高度
    public int height(TreeNode root){
        if (root==null){
            return 0;
        }
        int left = height(root.left);
        int right = height(root.right);
        return left>right?left+1:right + 1;

    }

    public boolean isBalanced0(TreeNode root) {
        if (root==null){
            return true;
        }
        int leftHeight = height(root.left);
        int rightHeight = height(root.right);
        return Math.abs(leftHeight-rightHeight)<=1
                && isBalanced0(root.left)
                && isBalanced0(root.right);
    }

    public int height1(TreeNode root){
        if (root==null){
            return 0;
        }
        int left = height1(root.left);
        int right = height1(root.right);

        if (left>=0 && right>=0 && Math.abs(left-right)<=1){
            return Math.max(left,right)+1;
        }else {
            return -1;
        }

    }

    public boolean isBalanced(TreeNode root) {
        if (root==null){
            return true;
        }
        return height1(root)>=0;
    }




    public boolean isSChild(TreeNode p,TreeNode q){
        if (p==null&&q!=null || p!=null&&q==null){
            return false;
        }
        if (p==null&&q==null){
            return true;
        }
        if (p.val!=q.val){
            return false;
        }
        return isSChild(p.left,q.right) && isSChild(p.right,q.left);


    }



    public boolean isSymmetric(TreeNode root) {

        if (root==null){
            return true;
        }
        return isSChild(root.left,root.right);


    }


    //分层遍历
    public void levleOrder(TreeNode root){
        if (root==null){
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            System.out.print(cur.val+" ");
            if (cur.left!=null){
                queue.offer(cur.left);
            }
            if (cur.right!=null){
                queue.offer(cur.right);
            }
        }
    }

    public List<List<TreeNode>> levelOrder(TreeNode root){
        List<List<TreeNode>> ret = new ArrayList<>();

        if (root==null){
            return ret;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int i = 0;
        while (!queue.isEmpty()){
            int size = queue.size();
            List<TreeNode> tmp = new ArrayList<>();
            while (size!=0){
                TreeNode cur = queue.poll();
                tmp.add(cur);
                size--;
                if (cur.left!=null){
                    queue.offer(cur.left);
                }
                if (cur.right!=null){
                    queue.offer(cur.right);
                }
            }
            ret.add(tmp);

        }
        return ret;
    }


    //判断二叉树是不是完全二叉树
    public boolean isCompleteTree(TreeNode root){
        if (root==null){
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if (cur!=null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                //此时遇到了null
                break;
            }
        }
        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if (cur!=null){
                return false;
            }
        }
        return true;



    }


    //找到两个节点的最近两个公共祖先
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root==null){
            return null;
        }
        if (root==p || root ==q){
            return root;
        }
        TreeNode leftTree = lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree = lowestCommonAncestor(root.right,p,q);

        if (leftTree!=null && rightTree!=null){
            return root;
        }else if(leftTree!=null){
            return leftTree;
        }else {
            return rightTree;
        }



    }


    public boolean getPath(TreeNode root, TreeNode node, Stack<TreeNode> stack){
        if (root==null || node == null){
            return false;
        }
        stack.push(root);
        if (root==node){
            return true;
        }
        boolean flag = getPath(root.left,node,stack);
        if (flag == true){
            return true;
        }
        boolean flag2 = getPath(root.right,node,stack);
        if (flag2 == true){
            return true;
        }

        stack.pop();
        return false;
    }

    public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
        if (root==null){
            return null;
        }
        Stack<TreeNode> stackP = new Stack<>();
        Stack<TreeNode> stackQ = new Stack<>();
        getPath(root,p,stackP);
        getPath(root,q,stackQ);

        int sizep = stackP.size();
        int sizeq = stackQ.size();

        if(sizep>sizeq){
            int size = sizep-sizeq;
            while (size!=0){
                stackP.pop();
                size--;
            }
        }else {
            int size = sizeq-sizep;
            while (size!=0){
                stackQ.pop();
                size--;
            }
        }

        while (!stackP.isEmpty() && !stackQ.isEmpty()){
            if (stackP.peek().equals(stackQ.peek())){
                return stackP.peek();
            }
            stackP.pop();
            stackQ.pop();
        }
        return null;

    }



    //根据前序，中序构造树
    /*

     public int priIndex; //先序遍历从前往后走


      public TreeNode buildTree(int[] preorder, int[] inorder) {


        return buildTreeChild(preorder, inorder , 0 , inorder.length-1);

    }

    private TreeNode buildTreeChild(int[] preorder,int[] inorder,
                                    int inbegin,int inend){

        //1.没有左树或没有右树
        if(inbegin > inend){
            return null;
        }
        //2.创建根节点
        TreeNode root = new TreeNode(preorder[priIndex]);

        //3.找到中序遍历中的根节点所在的下标
        int rootIndex = findIndex(inorder,inbegin,inend,preorder[priIndex]);
        if(rootIndex == -1){
            return null;
        }

        priIndex++;

        //创建左子树和右子树
        root.left = buildTreeChild(preorder,inorder,inbegin,rootIndex-1) ;
        root.right = buildTreeChild(preorder,inorder,rootIndex+1,inend);

        return root;






    }


    private int findIndex(int[] inorder, int inbegin,int inend,int key){
        for(int i = inbegin;i<=inend;i++){
            if(inorder[i] == key){
                return i;
            }
        }
        return -1;
    }
     */


    //根据中序，后序 构建二叉树
    /*


    public int postIndex;   //后序遍历从后往前走
    public TreeNode buildTree(int[] inorder, int[] postorder) {

        postIndex = postorder.length-1;
        return buildTreeChild(postorder, inorder , 0 , inorder.length-1);

    }

    private TreeNode buildTreeChild(int[] postorder,int[] inorder,
                                    int inbegin,int inend){

        //1.没有左树或没有右树
        if(inbegin > inend){
            return null;
        }
        //2.创建根节点
        TreeNode root = new TreeNode(postorder[postIndex]);

        //3.找到中序遍历中的根节点所在的下标
        int rootIndex = findIndex(inorder,inbegin,inend,postorder[postIndex]);
        if(rootIndex == -1){
            return null;
        }

        postIndex--;

        //创建左子树和右子树  先创建右子树
        root.right = buildTreeChild(postorder,inorder,rootIndex+1,inend);
        root.left = buildTreeChild(postorder,inorder,inbegin,rootIndex-1) ;


        return root;






    }


    private int findIndex(int[] inorder, int inbegin,int inend,int key){
        for(int i = inbegin;i<=inend;i++){
            if(inorder[i] == key){
                return i;
            }
        }
        return -1;
    }
     */



//    public List<Integer> preOrderNor(TreeNode root){
//        List<Integer> list = new ArrayList<>();
//        if (root == null){
//            return null;
//        }
//        Stack<TreeNode> stack = new Stack<>();
//        TreeNode cur = root;
//        TreeNode top = null;
//        while (cur!=null || !stack.isEmpty()){
//            while (cur!=null){
//                stack.push(cur);
//                list.add(cur.val);
//                cur = cur.left;
//            }
//
//            top = stack.pop();
//            cur = top.right;
//        }
//        return list;
//
//    }



    public void inOrderNor(TreeNode root){
        if (root==null){
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode top = null;
        while (cur!=null || !stack.isEmpty()){
            while (cur!=null){
                stack.push(cur);
                cur = cur.left;
            }
            top = stack.pop();
            System.out.print(top.val+" ");
            cur = top.right;
        }

    }


    public void postOrderNor(TreeNode root){
        if (root==null){
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode top = null;
        TreeNode prev = null;
        while (cur!=null || !stack.isEmpty()){
            while (cur!=null){
                stack.push(cur);
                cur = cur.left;
            }
            top = stack.peek();
            if (top.right==null || top.right == prev) {
                stack.pop();
                System.out.println(top.val);
                prev = top; // 表示当前这节点被打印了
            }else {
                cur = top.right;
            }

        }

    }




}
